﻿using log4net;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Timesheet.Data.Infrastructure;
using Timesheet.Data.Repository;
using Timesheet.Domain;

namespace Timesheet.Service
{
    public class TimeSheetDetailsService : ITimeSheetDetailsService
    {
        private readonly ITimeSheetDetailsRepository _Repository;
        private readonly IUnitOfWork _UnitOfWork;
        private readonly ITimeSheetDetailsNoteRepository _NoteRepository;
        public ILog Logger { get; private set; }

        public TimeSheetDetailsService(ITimeSheetDetailsRepository TimeSheetDetailsRepository, ITimeSheetDetailsNoteRepository timeSheetDetailsNoteRepository, IUnitOfWork unitOfWork)
        {
            this._NoteRepository = timeSheetDetailsNoteRepository;
            this._Repository = TimeSheetDetailsRepository;
            this._UnitOfWork = unitOfWork;
        }

        #region ITimeSheetDetailsService Members

        public IList<TimeSheetDetails> GetTimeSheetDetails()
        {
            return this._Repository.GetAll().ToList();
        }

        public bool CheckExist(TimeSheetDetails entity)
        {
            throw new NotImplementedException();
        }

        public TimeSheetDetails GetByID(Int64 id)
        {
            return this._Repository.GetById(id);
        }

        public void Add(TimeSheetDetails entity)
        {
            entity.TotalOT = entity.SunOTHours + entity.MonOTHours + entity.TueOTHours + entity.WebOTHours + entity.ThuOTHours + entity.FirOTHours + entity.SatOTHours;
            entity.TotalST = entity.SunSTHours + entity.MonSTHours + entity.TueSTHours + entity.WebSTHours + entity.ThuSTHours + entity.FriSTHours + entity.SatSTHours;
            if (string.IsNullOrEmpty(entity.Mileage))
                entity.Mileage = "";
            this._Repository.Add(entity);
        }

        public void Update(TimeSheetDetails entity)
        {
            entity.TotalOT = entity.SunOTHours+ entity.MonOTHours + entity.TueOTHours + entity.WebOTHours + entity.ThuOTHours + entity.FirOTHours + entity.SatOTHours;
            entity.TotalST = entity.SunSTHours+ entity.MonSTHours + entity.TueSTHours + entity.WebSTHours + entity.ThuSTHours + entity.FriSTHours + entity.SatSTHours;
            if (string.IsNullOrEmpty(entity.Mileage))
                entity.Mileage = "";
            this._Repository.Update(entity);
        }

        public void Delete(TimeSheetDetails entity)
        {
            this._Repository.Delete(entity);
        }            
       
        public IList<TimeSheetDetails> GetTimeSheetDetails(long tmshID)
        {
            return _Repository.GetQuery().Where(x => x.TransID == tmshID).ToList();
        } 
       

        #endregion

        public List<TimeSheetDetailsNote> GetListNotes(long timeSheetDetailsId)
        {
            return this._NoteRepository.GetQuery().Where(p=>p.TimeSheetDetailsId ==timeSheetDetailsId).ToList() ;
        }


        public void Add(TimeSheetDetailsNote entity)
        {
            this._NoteRepository.Add(entity);
        }

        public void Update(TimeSheetDetailsNote entity)
        {
            TimeSheetDetailsNote domain = _NoteRepository.GetById(entity.Id);
            if (domain != null)
            {
                domain.Notes = entity.Notes;
                domain.Note1 = entity.Note1;
                domain.Note2 = entity.Note2;
                domain.Note3 = entity.Note3;
                domain.Note4 = entity.Note4;
                domain.Note5 = entity.Note5;
                domain.Note6 = entity.Note6;
                domain.Note7 = entity.Note7;
                this._NoteRepository.Update(domain);
            }
        }

        public void Delete(TimeSheetDetailsNote entity)
        {
            TimeSheetDetailsNote domain = _NoteRepository.GetById(entity.Id);
            if (domain != null)
            {
                this._NoteRepository.Delete(domain);
            }
            
        }
    }
}
